/**
 * created by 小卷毛, 2019/08/30 0021
 * Copyright (c) 2019, 416143467@qq.com All Rights Reserved.
 * #                   *********                            #
 * #                  ************                          #
 * #                  *************                         #
 * #                 **  ***********                        #
 * #                ***  ****** *****                       #
 * #                *** *******   ****                      #
 * #               ***  ********** ****                     #
 * #              ****  *********** ****                    #
 * #            *****   ***********  *****                  #
 * #           ******   *** ********   *****                #
 * #           *****   ***   ********   ******              #
 * #          ******   ***  ***********   ******            #
 * #         ******   **** **************  ******           #
 * #        *******  ********************* *******          #
 * #        *******  ******************************         #
 * #       *******  ****** ***************** *******        #
 * #       *******  ****** ****** *********   ******        #
 * #       *******    **  ******   ******     ******        #
 * #       *******        ******    *****     *****         #
 * #        ******        *****     *****     ****          #
 * #         *****        ****      *****     ***           #
 * #          *****       ***        ***      *             #
 * #            **       ****        ****                   #
 */
package com.zhiyou.aifeng.common.base

import android.annotation.SuppressLint
import android.content.Context
import android.content.Intent
import android.net.Uri
import android.view.MotionEvent
import android.view.View
import android.widget.TextView
import androidx.annotation.ColorRes
import androidx.annotation.DrawableRes
import androidx.annotation.IdRes
import androidx.core.content.ContextCompat
import androidx.fragment.app.Fragment
import com.jakewharton.rxbinding3.view.clicks
import com.jakewharton.rxbinding3.view.touches
import java.util.concurrent.TimeUnit

inline fun <reified T : View> T.visible() {
    visibility = View.VISIBLE
}

inline fun <reified T : View> T.invisible() {
    visibility = View.INVISIBLE
}

inline fun <reified T : View> T.gone() {
    visibility = View.GONE
}

inline fun <reified T : View> T.isVisble() = visibility == View.VISIBLE

inline fun <reified T : View> View.find(@IdRes id: Int): T = findViewById(id)

inline fun <reified T : TextView> T.clear() {
    text = ""
}

fun Context.getColorEx(@ColorRes id: Int) = ContextCompat.getColor(this, id)

fun Context.getDrawableEx(@DrawableRes id: Int) = ContextCompat.getDrawable(this, id)!!

/**
 * 跳转到拨号页面
 */
fun Context.makeCall(number: String) =
    try {
        startActivity(
            Intent(
                Intent.ACTION_DIAL,
                Uri.parse("tel:$number")
            ).apply {
                flags = Intent.FLAG_ACTIVITY_NEW_TASK
            }
        )
        true
    } catch (e: Exception) {
        e.printStackTrace()
        false
    }

/**
 * 跳转到拨号页面
 */
fun Fragment.makeCall(number: String) =
    try {
        startActivity(
            Intent(
                Intent.ACTION_DIAL,
                Uri.parse("tel:$number")
            ).apply {
                flags = Intent.FLAG_ACTIVITY_NEW_TASK
            }
        )
        true
    } catch (e: Exception) {
        e.printStackTrace()
        false
    }

/**
 * 跳转到应用市场详情页面
 *
 * @param packageName 应用包名
 * @param marketPackage 应用市场包名{ @link ToolsExt.kt }, 为空时系统弹出选择列表
 */
fun Context.goToMarket(packageName: String, marketPackage: String = "") =
    try {
        startActivity(
            Intent(
                Intent.ACTION_VIEW,
                Uri.parse("market://details?id=$packageName")
            ).apply {
                if (marketPackage.isNotEmpty()) {
                    `package` = marketPackage
                }
                flags = Intent.FLAG_ACTIVITY_NEW_TASK
            }
        )
        true
    } catch (e: Exception) {
        e.printStackTrace()
        false
    }

/**
 * RxBinding3.0 防抖动点击事件，默认时间1000ms，默认单位毫秒
 */
@SuppressLint("CheckResult")
inline fun <reified T : View> T.oneClick(
    duration: Long = 1000,
    unit: TimeUnit = TimeUnit.MILLISECONDS,
    crossinline listener: (View) -> Unit
) {
    clicks()
        .throttleFirst(duration, unit)
        .subscribe { listener.invoke(this) }
}

/**
 * RxBinding3.0 防抖动触摸事件，默认时间1000ms，默认单位毫秒
 */
@SuppressLint("CheckResult")
inline fun <reified T : View> T.oneTouch(
    duration: Long = 1000,
    unit: TimeUnit = TimeUnit.MILLISECONDS,
    crossinline handle: (MotionEvent) -> Boolean = { true },
    crossinline listener: (MotionEvent) -> Unit = {}
) {
    touches { handle(it) }
        .throttleFirst(duration, unit)
        .subscribe { listener.invoke(it) }
}

/**
 * Iterate the receiver [List] using an index.
 */
inline fun <T> List<T>.forEachWithIndex(function: (Int, T) -> Unit) {
    for (index in 0 until size) function(index, get(index))
}

